package com.sfx.concurrent.demo.线程基础.线程的创建;

import lombok.extern.slf4j.Slf4j;

@Slf4j(topic = "c.CreateDemo")
public class CreateDemo {
    public static final int MAX_TURN = 5;

    public static String getCurThreadName() {
        return Thread.currentThread().getName();
    }

    //线程的编号
    static int threadNo = 1;

    static class DemoThread extends Thread { //①
        public DemoThread() {
            super("DemoThread-" + threadNo++); //②
        }

        public void run() { //③
            for (int i = 1; i < MAX_TURN; i++) {
                log.info(getName() + ", 轮次：" + i);
            }
            log.info(getName() + " 运行结束.");
        }
    }

    public static void main(String[] args) {
        Thread thread = null;
        //方法一：使用Thread子类创建和启动线程
        for (int i = 0; i < 2; i++) {
            thread = new DemoThread();
            thread.start();
        }
        log.info(getCurThreadName() + " 运行结束.");
    }
}